package com.tdam2011.grupo06;


import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class Principal extends Activity{

    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView lv = (ListView)findViewById(R.id.list);           
        ContactoAdapter adapter = new ContactoAdapter(crearContactos());             
        lv.setAdapter(adapter);
        
        lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Principal.this, ActivityAcciones.class);
				startActivity(intent);	
			}
        });
    }
         
    private ArrayList<Contacto> crearContactos() {
        ArrayList<Contacto> contactos = new ArrayList<Contacto>();
             
        contactos.add(new Contacto("Ashllian, Ary"));
        contactos.add(new Contacto("Bianco, Mateo")); 
        contactos.add(new Contacto("Borio, Marcos"));  
        contactos.add(new Contacto("Carnicero, Emiliano"));  
        contactos.add(new Contacto("Gigy, Diego"));  
        contactos.add(new Contacto("Miranda, Luis"));  
        contactos.add(new Contacto("Morales, Rodrigo"));  
        contactos.add(new Contacto("Rosso, Franco"));  
        contactos.add(new Contacto("Vittar, Pablo")); 
        
        return contactos;
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_settings:     
            	Intent intent = new Intent(Principal.this,
               	      PreferenciasContactos.class);
               	      startActivity(intent);
            break;
        }
        return true;
    }
    
    class Contacto{
    	protected String nombre;
    	
    	public Contacto(String nombre){
    		this.nombre=nombre;
    	}
    	
    	public String getNombre(){
    		return this.nombre;
    	}
    	
    	public void setNombre(String nombre){
    		this.nombre = nombre;
    	}
    }
    
    class Holder{
    	TextView txtNombreContacto;
    }
    
    class ContactoAdapter extends BaseAdapter{

    	private ArrayList<Contacto> _contactos;
		private LayoutInflater _inflater;
		
		public ContactoAdapter(ArrayList<Contacto> contactos){
			_contactos = contactos;
			_inflater = LayoutInflater.from(Principal.this);
		}
		
		public Contacto getContacto(int position) {
			Contacto contacto = (Contacto) getItem(position);
			return contacto;
		}
		
		public int getCount() {
			// TODO Auto-generated method stub
			return _contactos.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return _contactos.get(position);
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
	         
			Holder holder = null;
			if (convertView == null) {
				convertView = _inflater.inflate(R.layout.contacto_item, null);
				holder = new Holder();
				
				holder.txtNombreContacto = (TextView) convertView
						.findViewById(R.id.textView1);
				
				ImageButton button = (ImageButton) convertView.findViewById(R.id.imageButton1);
				button.setFocusable(false);
				
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}

			Contacto contacto = _contactos.get(position);

			holder.txtNombreContacto.setText(contacto.getNombre());

			return convertView;
		}
	}
}